package mammuthus.web.service

import net.liftweb.{json => SJSon}


/**
 * 6/7/15 WilliamZhu(allwefantasy@gmail.com)
 */
trait HBaseConfigure {
  this: SlaveService =>


  def startHbaseMaster(appName: String, action: String = "start") = {
    val nodes = getAPPExtra(appName, "nameNodes")
    val shellCommand = encode( s"""${installPath(appName)}/$appName/bin/hbase-daemon.sh --config "${installPath(appName)}/$appName/conf"  ${action} master""")
    haddoopC(appName, nodes, shellCommand)
  }

  def addHbaseSlaves(appName: String, slaves: String) = {
    val nameNode = getAPPExtra(appName, "nameNodes").split(",")(0)
    val originSize = getAPPExtraItems(appName, "appName").size
    val sres = new scala.collection.mutable.ArrayBuffer[Map[String, AnyRef]]()
    sres += Map("name" -> s"install ${appName} in new slaves", "rs" -> installNewSlaves(appName, slaves))
    //准备配置文件
    sres ++= configNewSlaves(appName,nameNode,slaves,List("hdfs-site.xml", "core-site.xml","hbase-site.xml"),"conf")

    //等待新的服务器上报到master
    waitUntilAllNewSlavesReady(appName,originSize,slaves)

    //更新节点信息
    val dataNodesNow = getAPPExtra(appName, "dataNodes").split(",").toSet ++ slaves.split(",").filterNot(f => f.isEmpty).toSet
    putAPPExtra(appName, Map("dataNodes" -> dataNodesNow.mkString(",")))
    putAPPExtra(appName, Map("nameNodes" -> getAPPExtra(appName, "nameNodes")))

    sres

  }

  def startHbaseSlaves(appName: String, action: String = "start") = {
    val nodes = getAPPExtra(appName, "dataNodes")
    val shellCommand = encode( s"""${installPath(appName)}/$appName/bin/hbase-daemon.sh --config "${installPath(appName)}/$appName/conf"  ${action} regionserver""")
    haddoopC(appName, nodes, shellCommand)
  }

  def startSpecificHbaseSlaves(appName: String, slaves:String,action: String = "start") = {
    val shellCommand = encode( s"""${installPath(appName)}/$appName/bin/hbase-daemon.sh --config "${installPath(appName)}/$appName/conf"  ${action} regionserver""")
    haddoopC(appName, slaves, shellCommand)
  }


}
